<?php
namespace Home\Controller;
use Think\Controller;
class RouterController extends CommonController {
    public function _initialize()
    {
        parent::_initialize();
        Vendor('Wxmpay.Pubhelper');
        Vendor('Wxmpay.Rexception');
        $this->wechatApi = D('WechatApi');
    }    
    public function home() {
        $jsApi = new \JsApi_pub();
		if (empty($_SESSION['wechat_openid']) && isWXBrowser()) {
			if (!isset($_GET['code'])) {
				$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
				$url = $jsApi->createAuthUserUrlForCode(urlencode($url));
				sys_debug_log("***********index createAuthUserUrlForCode***********************");
				sys_debug_log($url);
				Header("Location: $url");
			} else {
				$code = $_GET['code'];
				$wat = $this->wechatApi->getWebAccessToken($code);
				if (!empty($wat['access_token']) && !empty($wat['openid'])) {
					$this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
				}
			}
		}
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $wat = $this->wechatApi->getWebAccessToken($code);
            if (!empty($wat['access_token']) && !empty($wat['openid'])) {
                $this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
            }
        }
		
		// 微信联合登录
        if(!empty($_SESSION['wechat_openid']) && I('session.is_login',false)==false){
            $wechatUserInfo = D('WechatUserInfo');
            $wxinfo = $wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->find();
            if(!empty($wxinfo['user_id'])){
                $this->doLogin($wxinfo['user_id']);
            }
        }
		// 微信与user_id绑定
		if(!empty($_SESSION['wechat_openid']) && I('session.is_login',false)==true){
            $wechatUserInfo = D('WechatUserInfo');
            $wxinfo = $wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->save(array('user_id'=>I('session.user_id')));
        }
        $this->display('Index/index');
    }
    public function gpon() {
         $jsApi = new \JsApi_pub();
		if (empty($_SESSION['wechat_openid']) && isWXBrowser()) {
			if (!isset($_GET['code'])) {
				$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
				$url = $jsApi->createAuthUserUrlForCode(urlencode($url));
				sys_debug_log("***********index createAuthUserUrlForCode***********************");
				sys_debug_log($url);
				Header("Location: $url");
			} else {
				$code = $_GET['code'];
				$wat = $this->wechatApi->getWebAccessToken($code);
				if (!empty($wat['access_token']) && !empty($wat['openid'])) {
					$this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
				}
			}
		}
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $wat = $this->wechatApi->getWebAccessToken($code);
            if (!empty($wat['access_token']) && !empty($wat['openid'])) {
                $this->wechatApi->getSnsapiUserinfo($wat['access_token'], $wat['openid']);
            }
        }
		// 微信联合登录
        if(!empty($_SESSION['wechat_openid']) && I('session.is_login',false)==false){
            $wechatUserInfo = D('WechatUserInfo');
            $wxinfo = $wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->find();
            if(!empty($wxinfo['user_id'])){
                $this->doLogin($wxinfo['user_id']);
            }
        }
		// 微信与user_id绑定
		if(!empty($_SESSION['wechat_openid']) && I('session.is_login',false)==true){
            $wechatUserInfo = D('WechatUserInfo');
            $wxinfo = $wechatUserInfo->where("openid = '".$_SESSION['wechat_openid']."'")->save(array('user_id'=>I('session.user_id')));
        }
        $this->display('Index/index');
    }
    public function detail() {
        $this->display('Index/index');
    }
    public function cart() {
        $this->display('Index/index');
    }
    public function center() {
        $this->display('Index/index');
    }
    public function center_setting() {
        $this->display('Index/index');
    }
    public function center_setting_pwdmodify() {
        $this->display('Index/index');
    }
    public function ordercenter() {
        $this->display('Index/index');
    }
    public function orderdetail() {
        $this->display('Index/index');
    }
    public function order() {
        $this->display('Index/index');
    }
    public function express() {
        $this->display('Index/index');
    }
    public function expressOp() {
        $this->display('Index/index');
    }
    public function pay() {
        $this->display('Index/index');
    }
    public function tuixian() {
        $this->display('Index/index');
    }
    public function category() {
        $this->display('Index/index');
    }
}
